package threadPool;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: javase
 * @description: CPU密集型，IO密集型
 * @author : lld
 * @create: 2021-04-20 20:40
 */
public class MyPool {
    public static void main(String[] args) {
        /**
         * CPU密集型
         *
         * 设置最大线程数=cpu线程数
         *
         * IO密集型
         *
         * 设置最大线程数>判断程序中十分耗IO的线程
         **/
//      CPU获取线程数
        int processors = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                3,
                processors,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                new ThreadPoolExecutor.DiscardPolicy());
        for (int i = 0; i < 10; i++) {
            executor.execute(() -> System.out.println(Thread.currentThread().getName()));
        }
        executor.shutdown();
    }
}
